#!/bin/sh
# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2024-present ArchR (https://github.com/archr-linux/Arch-R)

# Simple script to watch the battery capacity and
# turn the power LED to different states:
# Blinking red when charge is less than 20%
# Blue turns off when charge is less than 10%

RED_TRIGGER=/sys/class/leds/red\:charging/trigger
BLUE_TRIGGER=/sys/class/leds/blue\:status/trigger

function red_led() {
  case $1 in
    none) echo none > ${RED_TRIGGER};;
    blink) echo timer > ${RED_TRIGGER};;
    steady) echo default-on > ${RED_TRIGGER};;
  esac
}

function blue_led() {
  case $1 in
    none) echo none > ${BLUE_TRIGGER};;
    blink) echo timer > ${BLUE_TRIGGER};;
    steady) echo default-on > ${BLUE_TRIGGER};;
  esac
}


while true
do
  CAP=$(cat /sys/class/power_supply/battery/capacity)
  STAT=$(cat /sys/class/power_supply/battery/status)
  if [[ ${STAT} == "Discharging" ]]; then
    if (( ${CAP} <= 10 )); then
      blue_led none
      red_led blink
      continue
    elif (( ${CAP} <= 20 )); then
      blue_led steady
      red_led blink
    else
      blue_led steady
      red_led none
    fi
  elif (( ${CAP} <= 95 )); then
    blue_led steady
    red_led steady
  else
    blue_led steady
    red_led none
  fi
  sleep 15
done
